package com.xzqwjw.taskmanager.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xzqwjw.taskmanager.service.DatabaseService;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PhotoDao implements DatabaseService {

  private static SQLiteDatabase objDB;
  private static String strDBFile = "";

  public PhotoDao(String strDBFile) {
    com.leaderinfo.dao.PhotoDao.strDBFile = strDBFile;
  }

  @Override
  public Map<String, Object> getById(int intAID) {
    InfoPhoto objPhoto = null;
    Map<String, Object> mapInfo = null;

    objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

    String strSQL = "SELECT * FROM info_cadre_photo WHERE aid=?";
    String[] aryArgs = {String.valueOf(intAID)};
    Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
    if (objCursor.moveToFirst()) {
      objPhoto = new InfoPhoto();
      getInfo(objCursor, objPhoto);
      mapInfo = new HashMap<String, Object>();
      mapInfo.put("PhotoInfo", objPhoto);
    }
    objCursor.close();
    objDB.close();
    return mapInfo;
    // return null;
  }

  @Override
  public Map<String, Object> getByIdBasic(int intBasicID) {
    InfoPhoto objPhoto = null;
    Map<String, Object> mapInfo = null;

    objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);

    String strSQL = "SELECT * FROM info_cadre_photo WHERE id_basic=?";
    String[] aryArgs = {String.valueOf(intBasicID)};
    Cursor objCursor = objDB.rawQuery(strSQL, aryArgs);
    if (objCursor.moveToFirst()) {
      objPhoto = new InfoPhoto();
      getInfo(objCursor, objPhoto);
      mapInfo = new HashMap<String, Object>();
      mapInfo.put("PhotoInfo", objPhoto);
    }
    objCursor.close();
    objDB.close();
    return mapInfo;
  }

  @Override
  public List<Map<String, Object>> listByIdBasic(int intBasicID) {
    return null;
  }

  public String writeImageToDisk() {
    String strReturn = "";
    int intBasicID = 0;
    byte[] bytePhoto = null;
    String strIDCardNumber = "";

    objDB = SQLiteDatabase.openOrCreateDatabase(strDBFile, null);
    DAOPartyInfo objPartyInfoDAO = new DAOPartyInfo(strDBFile);

    String strSQL = "SELECT * FROM info_cadre_photo";
    Cursor objCursor = objDB.rawQuery(strSQL, null);
    if (objCursor.getCount() > 0) {
      while (objCursor.moveToNext()) {
        intBasicID = objCursor.getInt(objCursor
            .getColumnIndex("id_basic"));
        bytePhoto = objCursor.getBlob(objCursor
            .getColumnIndex("photo_data"));
        strIDCardNumber = objPartyInfoDAO.getIDCardNumber(intBasicID);

        Utility.writeImageToDisk(bytePhoto, strIDCardNumber + ".jpg");
      }
      strReturn = "OK";
    } else {
      strReturn = "û��ͼ���ļ�������ϵϵͳ����Ա��";
    }
    objCursor.close();
    objDB.close();
    return strReturn;
  }

  private void getInfo(Cursor objCursor, InfoPhoto objPhotoInfo) {
    objPhotoInfo.AID = objCursor.getInt(objCursor.getColumnIndex("aid"));
    objPhotoInfo.BasicID = objCursor.getInt(objCursor
        .getColumnIndex("id_basic"));
    objPhotoInfo.PhotoData = objCursor.getBlob(objCursor
        .getColumnIndex("photo_data"));
    objPhotoInfo.PhotoUrl = objCursor.getString(objCursor
        .getColumnIndex("photo_url"));
  }

  @Override
  public List<Map<String, Object>> listByWhere(String strWhere) {
    return null;
  }

  @Override
  public List<Map<String, Object>> listAll() throws Exception {
    return null;
  }

}
